function DispatchCtrlLib() {}
import { DispatchRtcLib } from './DispatchRtcLib'
import { DispatchRtcLib_VoipVideo } from './DispatchRtcLib'
import { DispatchRtcLib_VoipAudio } from './DispatchRtcLib'
import { DispatchRtcLib_MoniterVideo } from './DispatchRtcLib'
import { DispatchDevices } from './DispatchRtcLib'
import { DispatchConference } from './DispatchRtcLib'
import { ScsConstants } from '../js/ScsConstants'
DispatchCtrlLib.callback = null;
var DispatchCtrl_UseTcpSip = 0;
DispatchCtrlLib.websocket = null;
DispatchCtrlLib.extnumber = "";
//内部默认回调
DispatchCtrlLib.FuncCallback = function(msg) {

	var parm = msg;
	if (typeof msg == "string")
	{
		parm = JSON.parse(msg);
		console.log("DispatchCtrlLib.FuncCallback: " + msg);
	}
	else
	{
		console.log("DispatchCtrlLib.FuncCallback: " + JSON.stringify(msg));
	}

	if (parm.Msg == "InitializeRsp" &&
	    typeof parm.ExtNumber == "string")
	{
		DispatchCtrlLib.extnumber = parm.ExtNumber;
	}

	if (parm.Msg == "LoginRsp" &&
	    parm.Result == 0 &&
		typeof parm.LoginInfo.ExtNumber == "number")
	{
		DispatchCtrlLib.extnumber = parm.LoginInfo.ExtNumber.toString();
	}

	if (DispatchCtrlLib.callback)
	{
		DispatchCtrlLib.callback(parm.Msg, parm);
	}
}

DispatchCtrlLib.WsSend = function(msg) {
	var str = JSON.stringify(msg);
    console.log("DispatchCtrlLib.WsSend: " + str);
    if (DispatchCtrlLib.websocket && DispatchCtrlLib.websocket.HasOpen)
    {
        DispatchCtrlLib.websocket.send(str);
    }
}

/************************** 初始化&登录 ******************************/
//初始化资源 返回非0时表示初始化媒体失败
DispatchCtrlLib.Initialize = function(callback, no_media) {

    if (typeof callback == "function")
    {
        DispatchCtrlLib.callback = callback;
    }


    if (typeof(no_media) == "number" && no_media == 1)
    {
        try
        {
            DispatchCtrlLib.NewWebsocket();
        }
        catch(e)
        {
            console.log(e)
        }
    }
    else
    {
		//等待DispatchRtcLib加载
        var interval = window.setInterval(function() {
            if (typeof DispatchRtcLib != "undefined")
            {
                DispatchRtcLib.Init();

                try
                {
                    DispatchCtrlLib.NewWebsocket();
                }
                catch(e)
                {
                    console.log(e)
                }

                clearInterval(interval);
            }
        }, 500);

        return 0;
    }

    return 0;
}

DispatchCtrlLib.WebsocketOnOpen = function(evt) {
    console.log("DispatchCtrlLib.WebsocketOnOpen");
    DispatchCtrlLib.websocket.HasOpen = true;

    var msg = new Object;
    msg.Msg = "InitializeReq";

    if (DispatchCtrlLib.ReconnectTimer)
    {
        clearInterval(DispatchCtrlLib.ReconnectTimer);
        DispatchCtrlLib.ReconnectTimer = null;

        msg.Type = 1;

        console.log("DispatchCtrlLib.Reconnect");

        var msgData = {
            MsgType: "ServerLinkStatusNotify",
            Parm: {
                Type : 1,
            },
        };
        DispatchCtrlLib.FuncCallback(msgData);
    }

	DispatchCtrlLib.WsSend(msg);
}

DispatchCtrlLib.NewWebsocket = function()
{
    console.log("new WebSocket");
	DispatchCtrlLib.websocket = new WebSocket("ws://127.0.0.1:3701");
    DispatchCtrlLib.websocket.binaryType = 'arraybuffer';
    DispatchCtrlLib.websocket.HasOpen = false;
    DispatchCtrlLib.websocket.onopen = DispatchCtrlLib.WebsocketOnOpen;
    DispatchCtrlLib.websocket.onclose = DispatchCtrlLib.WebsocketOnClose;
    DispatchCtrlLib.websocket.onmessage = DispatchCtrlLib.WebsocketOnMessage;
    DispatchCtrlLib.websocket.onerror = DispatchCtrlLib.WebsocketOnError;
}

DispatchCtrlLib.WebsocketOnClose = function(evt) {
    console.log("DispatchCtrlLib.WebsocketOnClose");
    if (DispatchCtrlLib.websocket.HasOpen){
        var msgData = {
            MsgType: "ServerLinkStatusNotify",
            Parm: {
                Type : 2,
            },
        };
		DispatchCtrlLib.FuncCallback(msgData);

        DispatchCtrlLib.ReconnectTimer = setInterval(DispatchCtrlLib.NewWebsocket, 5000);
    }
    else{
        //连接失败
        var msgData = {
            MsgType: "ServerLinkStatusNotify",
            Parm: {
                Result : 1,
            },
        };
        DispatchCtrlLib.FuncCallback(msgData);
    }
    DispatchCtrlLib.websocket.HasOpen = false;
}

DispatchCtrlLib.WebsocketOnMessage = function(evt) {
    if (typeof(evt.data) == "string") {
        console.log("DispatchCtrlLib.WebsocketOnMessage:" + evt.data);
        var parm = JSON.parse(evt.data);
        if (typeof(DispatchRtcLib) != "undefined")
        {
            if (DispatchRtcLib.OnMsgCallback(parm))
            {
                DispatchCtrlLib.FuncCallback(parm);
            }
        }
        else
        {
            DispatchCtrlLib.FuncCallback(parm);
        }
    }
}

DispatchCtrlLib.WebsocketOnError = function(evt) {
    console.log("DispatchCtrlLib.WebsocketOnError");
}

//释放资源
DispatchCtrlLib.Uninitialize = function() {
    clearInterval(DispatchCtrlLib.ReconnectTimer);
    DispatchCtrlLib.ReconnectTimer = null;
    DispatchCtrlLib.websocket.onclose = null;
    DispatchCtrlLib.websocket.close();
    return 0;
}

//登录
DispatchCtrlLib.Login = function(Name, Password, Server) {
    if (typeof Name == "string" && typeof Password == "string" && typeof Server == "string") {
        var msg = new Object;
        msg.Msg = "LoginReq";
        msg.Name = Name;
        msg.Password = Password;
        msg.Server = Server;
        DispatchCtrlLib.WsSend(msg);

        return 0;
    } else {
        return -1;
    }
}

//注销
DispatchCtrlLib.Logout = function() {

    var msg = new Object;
    msg.Msg = "LogoutReq";
    DispatchCtrlLib.WsSend(msg);

    return 0;
}

//登录界面跳转保活
DispatchCtrlLib.LoginHold = function() {

    var msg = new Object;
    msg.Msg = "LoginHoldReq";
    DispatchCtrlLib.WsSend(msg);

    return 0;
}

/************************** 监控视频 ******************************/
//拉取监控视频
DispatchCtrlLib.MonitorVideoPull = function(Number, Type) {
    if (typeof Number == "string" && typeof Type == "number") {
        var msg = new Object;
        msg.Msg = "MonitorVideoPullReq";
        msg.Number = Number;
        msg.Type = Type;
        msg.CallID = ScsConstants.VoipCallID++;

        var OnMakeCallSucess = function(sdp)
        {
            msg.OfferSdp = sdp;

            console.log("DispatchCtrlLib.MonitorVideoPull " + msg.CallID + " local_sdp:");
            console.log(sdp);

            DispatchCtrlLib.WsSend(msg);
        }

        var OnMakeCallFail = function(error)
        {
            console.log("DispatchCtrlLib.MonitorVideoPull Fail:" + error);

            var failmsg = new Object;
            failmsg.Msg = "MonitorVideoStatusNotify";
            failmsg.CallID = msg.CallID;
            failmsg.Status = 7;
            failmsg.Reason = error;

            DispatchCtrlLib.FuncCallback(failmsg);
        }

        return DispatchRtcLib.CreateOffer(msg.CallID, DispatchRtcLib_MoniterVideo, OnMakeCallSucess, OnMakeCallFail);


        DispatchCtrlLib.WsSend(msg);
        return msg.CallID;
    } else {
        return -1;
    }
}

//挂断监控视频
DispatchCtrlLib.MonitorVideoHangup = function(CallID) {
    if (typeof CallID == "number") {
        var msg = new Object;
        msg.Msg = "MonitorVideoHangupReq";
        msg.CallID = CallID;
        DispatchRtcLib.Stop(CallID);
        DispatchCtrlLib.WsSend(msg);
        return 0;
    } else {
        return -1;
    }
}

//监控视频推送来电处理
DispatchCtrlLib.MonitorVideoHandleIncomingCall = function(CallID, Handle) {
    if (typeof CallID == "number" && typeof Handle == "number") {
        var msg = new Object;
        msg.Msg = "MonitorVideoHandleIncomingCallReq";
        msg.CallID = CallID;
        msg.Handle = Handle;

        if (Handle == 1)
        {
            var OnMakeCallSucess = function(sdp)
            {
                msg.OfferSdp = sdp;

                console.log("DispatchCtrlLib.MonitorVideoHandleIncomingCall " + msg.CallID + " local_sdp:");
                console.log(sdp);

                DispatchCtrlLib.WsSend(msg);
            }

            var OnMakeCallFail = function(error)
            {
                console.log("DispatchCtrlLib.MonitorVideoHandleIncomingCall Fail:" + error);

                var failmsg = new Object;
                failmsg.Msg = "MonitorVideoStatusNotify";
                failmsg.CallID = msg.CallID;
                failmsg.Status = 7;
                failmsg.Reason = error;

                DispatchCtrlLib.FuncCallback(failmsg);
            }

            return DispatchRtcLib.CreateOffer(msg.CallID, DispatchRtcLib_MoniterVideo, OnMakeCallSucess, OnMakeCallFail);
        }
        else
        {
        	DispatchCtrlLib.WsSend(msg);
        }
        return 0;
    } else {
        return -1;
    }
}

//监控视频环境音静音
DispatchCtrlLib.MonitorVideoSetVolume = function(CallID, Volume) {
    if (typeof CallID == "number" && typeof Volume == "number") {
        return DispatchRtcLib.VoipSetCallVolume(CallID, Volume);
    } else {
        return -1;
    }
}

//监控视频转分发
DispatchCtrlLib.MonitorVideoPush = function(CallID, Number) {
    if (typeof CallID == "number" && typeof Number == "string") {
        var msg = new Object;
        msg.Msg = "MonitorVideoPushReq";
        msg.CallID = CallID;
        msg.Number = Number;
        DispatchCtrlLib.WsSend(msg);
        return 0;
    } else {
        return -1;
    }
}

//摄像头云台控制
DispatchCtrlLib.MonitorVideoPtz = function(Number, Command, Direction, Speed) {
    if (typeof Number == "string" && typeof Command == "number" && typeof Direction == "number" && typeof Speed ==
        "number") {
        var msg = new Object;
        msg.Msg = "MonitorVideoPtzReq";
        msg.Number = Number;
        msg.Command = Command;
        msg.Direction = Direction;
        msg.Speed = Speed;
        DispatchCtrlLib.WsSend(msg);
        return 0;
    } else {
        return -1;
    }
}

// var VoipCallID = 100;

/************************** IP通话 ******************************/
//IP语音通话
DispatchCtrlLib.VoipMakeAudioCall = function(ExtNum, DcPhone) {
    if (typeof ExtNum == "string") {
        var msg = new Object;
        msg.Msg = "VoipMakeAudioCallReq";
        msg.ExtNum = ExtNum;

        if (typeof DcPhone == "number")
        {
            msg.CallID = DcPhone;

            DispatchCtrlLib.WsSend(msg);
        }
        else
        {
            msg.CallID = ScsConstants.VoipCallID++;

            var OnMakeCallSucess = function(sdp)
            {
                msg.OfferSdp = sdp;

                console.log("DispatchCtrlLib.VoipMakeAudioCall " + msg.CallID + " local_sdp:");
                console.log(sdp);

                DispatchCtrlLib.WsSend(msg);
            }

            var OnMakeCallFail = function(error)
            {
                console.log("DispatchCtrlLib.VoipMakeAudioCall Fail:" + error);

                var failmsg = new Object;
                failmsg.Msg = "VoipCallStatusNotify";
                failmsg.CallID = msg.CallID;
                failmsg.Status = 7;
                failmsg.Reason = error;

                DispatchCtrlLib.FuncCallback(failmsg);
            }

            return DispatchRtcLib.CreateOffer(msg.CallID, DispatchRtcLib_VoipAudio, OnMakeCallSucess, OnMakeCallFail);
        }

        return msg.CallID;
    } else {
        return -1;
    }
}

//IP视频通话
DispatchCtrlLib.VoipMakeVideoCall = function(ExtNum) {
    if (typeof ExtNum == "string") {
        var msg = new Object;
        msg.Msg = "VoipMakeVideoCallReq";
        msg.ExtNum = ExtNum;
        msg.CallID = ScsConstants.VoipCallID++;

        var OnMakeCallSucess = function(sdp)
        {
            msg.OfferSdp = sdp;

            console.log("DispatchCtrlLib.VoipMakeVideoCall " + msg.CallID + " local_sdp:");
            console.log(sdp);

            DispatchCtrlLib.WsSend(msg);
        }

        var OnMakeCallFail = function(error)
        {
            console.log("DispatchCtrlLib.VoipMakeVideoCall Fail:" + error);

            var failmsg = new Object;
            failmsg.Msg = "VoipCallStatusNotify";
            failmsg.CallID = msg.CallID;
            failmsg.Status = 7;
            failmsg.Reason = error;

            DispatchCtrlLib.FuncCallback(failmsg);
        }

        return DispatchRtcLib.CreateOffer(msg.CallID, DispatchRtcLib_VoipVideo, OnMakeCallSucess, OnMakeCallFail);
    } else {
        return -1;
    }
}

//IP通话挂断
DispatchCtrlLib.VoipHangupCall = function(CallID) {
    if (typeof CallID == "number") {
        var msg = new Object;
        msg.Msg = "VoipHangupCallReq";
        msg.CallID = CallID;

        DispatchCtrlLib.WsSend(msg);

        DispatchRtcLib.Stop(CallID);

        return 0;
    } else {
        return -1;
    }
}

//IP通话来电处理
DispatchCtrlLib.VoipHandleIncomingCall = function(CallID, Handle) {
    if (typeof CallID == "number" && typeof Handle == "number") {
        var msg = new Object;
        msg.Msg = "VoipHandleIncomingCallReq";
        msg.Handle = Handle;
        msg.CallID = CallID;

		if (Handle == 1)
		{
			var OnCreateAnswerSucess = function(sdp)
			{
			    msg.AnswerSdp = sdp;

			    console.log("DispatchCtrlLib.VoipHandleIncomingCall " + msg.CallID + " local_sdp:");
			    console.log(sdp);

			    DispatchCtrlLib.WsSend(msg);
			}

			var OnCreateAnswerFail = function(error)
			{
			    var failmsg = new Object;
			    failmsg.Msg = "VoipCallStatusNotify";
			    failmsg.CallID = msg.CallID;
			    failmsg.Status = 7;
			    failmsg.Reason = error;

			    DispatchCtrlLib.FuncCallback(failmsg);
			}

			DispatchRtcLib.CreateAnswer(CallID, OnCreateAnswerSucess, OnCreateAnswerFail);

		}
		else
		{
			DispatchCtrlLib.WsSend(msg);
		}

        return 0;
    } else {
        return -1;
    }
}

//IP通话设置音量
DispatchCtrlLib.VoipSetCallVolume = function(CallID, Volume) {
    if (typeof CallID == "number" && typeof Volume == "number") {
        return DispatchRtcLib.VoipSetCallVolume(CallID, Volume);
    } else {
        return -1;
    }
}

//Integrated Camera - 摄像头设备名称
//none - 关闭摄像头，默认头像
//screen  - 屏幕共享
DispatchCtrlLib.VoipVideoSetSource = function(CallID, VideoSrc)
{
    if (typeof CallID == "number" && typeof VideoSrc == "string")
    {
        return DispatchRtcLib.VoipVideoSetSource(CallID, VideoSrc);
    }
    else
    {
        return -1;
    }
}

//通道设置声道
DispatchCtrlLib.VoipSetCallTrack = function(CallID, Track) {
    return -1; // 废弃
}

//IP通话禁麦
DispatchCtrlLib.VoipSetCallMicMute = function(CallID, Mute) {
    if (typeof CallID == "number" && typeof Mute == "number") {
        return DispatchRtcLib.VoipSetCallMicMute(CallID, Mute);
    } else {
        return -1;
    }
}

//IP通话呼叫转移
DispatchCtrlLib.VoipTransferCall = function(CallID, ExtNum) {
    if (typeof CallID == "number" && typeof ExtNum == "string") {
        var msg = new Object;
        msg.Msg = "VoipTransferCallReq";
        msg.ExtNum = ExtNum;
        msg.CallID = CallID;
        DispatchCtrlLib.WsSend(msg);
        return 0;
    } else {
        return -1;
    }
}

//IP通话呼叫保持
DispatchCtrlLib.VoipHoldCall = function(CallID, Type) {
    if (typeof CallID == "number" && typeof Type == "number") {
        var msg = new Object;
        msg.Msg = "VoipHoldCallReq";
        msg.Type = Type;
        msg.CallID = CallID;
        DispatchCtrlLib.WsSend(msg);
        return 0;
    } else {
        return -1;
    }
}

//IP调度:开始/结束监听终端状态
DispatchCtrlLib.VoipDispatchMoniterStatus = function(ExtNum, Type) {
    if (typeof ExtNum == "string" && typeof Type == "number") {
        var msg = new Object;
        msg.Msg = "VoipDispatchMoniterStatusReq";
        msg.Type = Type;
        msg.ExtNum = ExtNum;
        DispatchCtrlLib.WsSend(msg);
        return 0;
    } else {
        return -1;
    }
}

//IP调度:强插呼叫
DispatchCtrlLib.VoipDispatchInsertCall = function(ExtNum, DcPhone) {
    if (typeof ExtNum == "string") {
        var msg = new Object;
        msg.Msg = "VoipDispatchInsertCallReq";
        msg.ExtNum = ExtNum;
        if (typeof DcPhone == "number")
        {
            msg.CallID = DcPhone;
        }
        else
        {
            msg.CallID = ScsConstants.VoipCallID++;
        }
        DispatchCtrlLib.WsSend(msg);
        return msg.CallID;
    } else {
        return -1;
    }
}

//IP调度:强断呼叫
DispatchCtrlLib.VoipDispatchInterruptCall = function(ExtNum) {
    if (typeof ExtNum == "string") {
        var msg = new Object;
        msg.Msg = "VoipDispatchInterruptCallReq";
        msg.ExtNum = ExtNum;
        DispatchCtrlLib.WsSend(msg);
        return 0;
    } else {
        return -1;
    }
}

//IP调度:强拆呼叫
DispatchCtrlLib.VoipDispatchBreakCall = function(ExtNum, DcPhone) {
    if (typeof ExtNum == "string") {
        var msg = new Object;
        msg.Msg = "VoipDispatchBreakCallReq";
        msg.ExtNum = ExtNum;
		if (typeof DcPhone == "number")
		{
		    msg.CallID = DcPhone;
		}
		else
		{
		    msg.CallID = ScsConstants.VoipCallID++;
		}
        DispatchCtrlLib.WsSend(msg);
        return 0;
    } else {
        return -1;
    }
}

//IP调度:监听呼叫
DispatchCtrlLib.VoipDispatchMoniterCall = function(ExtNum, DcPhone) {
    if (typeof ExtNum == "string") {
        var msg = new Object;
        msg.Msg = "VoipDispatchMoniterCallReq";
        msg.ExtNum = ExtNum;
        if (typeof DcPhone == "number")
        {
            msg.CallID = DcPhone;
        }
        else
        {
            msg.CallID = ScsConstants.VoipCallID++;
        }
        DispatchCtrlLib.WsSend(msg);
        return msg.CallID;
    } else {
        return -1;
    }
}

//IP调度:呼叫转会议
DispatchCtrlLib.VoipDispatchCallToConference = function(ExtNum, ConfID) {
    if (typeof ExtNum == "string"&&typeof ConfID == "string") {
        var msg = new Object;
        msg.Msg = "VoipDispatchCallToConferenceReq";
        msg.ExtNum = ExtNum;
        msg.ConfID = ConfID;
        DispatchCtrlLib.WsSend(msg);
        return 0;
    } else {
        return -1;
    }
}

//IP调度:代接来电
DispatchCtrlLib.VoipDispatchSubstituteCall = function(ExtNum, DcPhone) {
    if (typeof ExtNum == "string") {
        var msg = new Object;
        msg.Msg = "VoipDispatchSubstituteCallReq";
        msg.ExtNum = ExtNum;
        if (typeof DcPhone == "number")
        {
            msg.CallID = DcPhone;
        }
        else
        {
            msg.CallID = ScsConstants.VoipCallID++;
        }
        DispatchCtrlLib.WsSend(msg);
        return msg.CallID;
    } else {
        return -1;
    }
}

//IP调度:设置台席话机
DispatchCtrlLib.VoipDispatchSetDcPhone= function(LeftExt, RightExt) {
    if (typeof LeftExt == "string" && typeof RightExt == "string") {
        var msg = new Object;
        msg.Msg = "VoipDispatchSetDcPhoneReq";
        msg.LeftExt = LeftExt;
        msg.RightExt = RightExt;
        DispatchCtrlLib.WsSend(msg);
        return msg.CallID;
    } else {
        return -1;
    }
}

//IP调度:接听台席队列来电
DispatchCtrlLib.VoipDispatchAnswerFifo = function(ExtNum, DcPhone) {
    if (typeof ExtNum == "string") {
        var msg = new Object;
        msg.Msg = "VoipDispatchAnswerFifoReq";
        msg.ExtNum = ExtNum;
        if (typeof DcPhone == "number")
        {
            msg.CallID = DcPhone;
        }
        else
        {
            msg.CallID = ScsConstants.VoipCallID++;
        }
        DispatchCtrlLib.WsSend(msg);
        return msg.CallID;
    } else {
        return -1;
    }
}

//IP调度:通话桥接
DispatchCtrlLib.VoipDispatchCallBridge = function(ExtNum, OtherExtNum) {
    if (typeof ExtNum == "string" && typeof OtherExtNum == "string") {
        var msg = new Object;
        msg.Msg = "VoipDispatchCallBridgeReq";
        msg.ExtNum = ExtNum;
        msg.OtherExtNum = OtherExtNum;
        DispatchCtrlLib.WsSend(msg);
        return msg.CallID;
    } else {
        return -1;
    }
}

//IP调度:来电闭铃
DispatchCtrlLib.VoipDispatchStopRing = function(DcPhone) 
{
   if (typeof DcPhone == "number") {
        var msg = new Object;
        msg.Msg = "VoipDispatchStopRingReq";
        msg.CallID = DcPhone;
        DispatchCtrlLib.WsSend(msg);
        return 0;
    } else {
        return -1;
    }
}

DispatchCtrlLib.VoipLastIncomingCall = function() {
    var msg = new Object;
    msg.Msg = "VoipLastIncomingCall";
    DispatchCtrlLib.WsSend(msg);
    return 0;
}

/************************** 会议 ******************************/
//当前会议信息
DispatchCtrlLib.CurrentConferenceInfo = function()
{
    return DispatchConference.CurrentConferenceInfo();
}

//加入会议
DispatchCtrlLib.ConferenceEnter = function(ConfID, Type, ConfPwd)
{
	if (typeof ConfID == "string" &&
		typeof Type == "number")
	{
		return DispatchConference.ConferenceEnter(ConfID, Type, ConfPwd);
	}

	return -1;
}

//接受/拒绝会议邀请
DispatchCtrlLib.ConferenceHandleInvite = function(Handle)
{
	if (typeof Handle == "number")
	{
		return DispatchConference.ConferenceHandleInvite(Handle);
	}

	return -1;
}

//离开会议
DispatchCtrlLib.ConferenceExit = function()
{
	return DispatchConference.ConferenceExit();
}

//会议邀请
DispatchCtrlLib.ConferenceInvite = function(ExtNumList, ConfID)
{
    for(var i=0;i<ExtNumList.length;i++)
	{
        if(typeof ExtNumList[i].ExtNum !== "string")
		{
			return -1;
        }
    }

	if (ExtNumList.length == 0)
	{
		return -1;
	}

	if (ConfID)
	{
		var msg = new Object;
		msg.Msg = "ConferenceInviteReq";
		msg.ConfID = ConfID;
		msg.ExtNumList = ExtNumList;
		DispatchCtrlLib.WsSend(msg);
		return 0;
	}

	return DispatchConference.ConferenceInvite(ExtNumList);
}

//会议结束
DispatchCtrlLib.ConferenceEnd = function()
{
    return DispatchConference.ConferenceEnd();
}

//会议成员移除
DispatchCtrlLib.ConferenceKick = function(ExtNum)
{
    if (typeof ExtNum == "string")
	{
        return DispatchConference.ConferenceKick(ExtNum);
    } else {
        return -1;
    }
}

//本地麦克风静音
DispatchCtrlLib.ConferenceLocalMicMute = function(Mute)
{
    return DispatchConference.ConferenceLocalMicMute(Mute);
}

//会议成员禁言
DispatchCtrlLib.ConferenceMute = function(ExtNum, Mute) {
    if (typeof ExtNum == "string" && typeof Mute == "number") {
        return DispatchConference.ConferenceMute(ExtNum, Mute);
    } else {
        return -1;
    }
}

//会议设置音量
DispatchCtrlLib.ConferenceSetVolume = function(Volume) {
    if (typeof Volume == "number") {
        return DispatchConference.ConferenceSetVolume(Volume);
    } else {
        return -1;
    }
}

//会议切换本地扬声器
DispatchCtrlLib.ConferenceSetSpeaker = function(DevLabel) {
    if (typeof DevLabel == "string") {
        return DispatchConference.ConferenceSetSpeaker(DevLabel);
    } else {
        return -1;
    }
}

//会议切换本地麦克风
DispatchCtrlLib.ConferenceSetMicphone = function(DevLabel) {
    if (typeof DevLabel == "string") {
        return DispatchConference.ConferenceSetMicphone(DevLabel);
    } else {
        return -1;
    }
}

//会议切换本地扬声器
DispatchCtrlLib.ConferenceSetCamera = function(DevLabel) {
    if (typeof DevLabel == "string") {
        return DispatchConference.ConferenceSetCamera(DevLabel);
    } else {
        return -1;
    }
}

//会议视频观看
DispatchCtrlLib.ConferenceWatch = function(ExtNum, VideoLib) {
    if (typeof ExtNum == "string" &&
		typeof VideoLib == "object") {
        return DispatchConference.ConferenceWatch(ExtNum, VideoLib);
    } else {
        return -1;
    }
}

//会议视频聚焦
DispatchCtrlLib.ConferenceFocus = function(ExtNum) {
    if (typeof ExtNum == "string") {
        return DispatchConference.ConferenceFocus(ExtNum);
    } else {
        return -1;
    }
}

//会议移交主持人
DispatchCtrlLib.ConferenceChangeHost = function(ExtNum) {
    if (typeof ExtNum == "string") {
        return DispatchConference.ChangeHost(ExtNum);
    } else {
        return -1;
    }
}

/************************** 对讲 ******************************/
//加载通道
DispatchCtrlLib.PttLoadChannel = function(CallMode, TargetInfo) {
    if (CallMode == 0) {
        if (typeof TargetInfo.GroupID == "number" && typeof TargetInfo.GroupNumber == "string" && typeof TargetInfo
            .GroupPriority == "number") {
            var msg = new Object;
            msg.Msg = "PttLoadChannelReq";
            msg.CallMode = CallMode;
            msg.GroupID = TargetInfo.GroupID;
            msg.GroupNumber = TargetInfo.GroupNumber;
            msg.GroupPriority = TargetInfo.GroupPriority;
            msg.ChannelID = ScsConstants.VoipCallID++;
            DispatchCtrlLib.WsSend(msg);
            return msg.ChannelID;
        } else {
            return -1;
        }
    } else if (CallMode == 1) {
        if (typeof TargetInfo.UserNumber == "string" && typeof TargetInfo.UserID == "number") {
            var msg = new Object;
            msg.Msg = "PttLoadChannelReq";
            msg.CallMode = CallMode;
            msg.UserNumber = TargetInfo.UserNumber;
            msg.UserID = TargetInfo.UserID;
            msg.ChannelID = ScsConstants.VoipCallID++;
            DispatchCtrlLib.WsSend(msg);
            return msg.ChannelID;
        } else {
            return -1;
        }
    } else if (CallMode == 2) {
        var typeG = 0;
        var typeU = 1;
        if(TargetInfo.GroupList.length>0){
            for(var i=0;i<TargetInfo.GroupList.length;i++) {
                if (typeof TargetInfo.GroupList[i].Number == "string") {
                    typeG = 1;
                }else{
                    return -1;
                }
            }
            if(TargetInfo.UserList.length>0) {
                for(var i=0;i<TargetInfo.UserList.length;i++) {
                    if (typeof TargetInfo.UserList[i].Number != "string") {
                        return -1;
                    }
                }
            }
        }else{
            if(TargetInfo.UserList.length>0) {
                for(var i=0;i<TargetInfo.UserList.length;i++) {
                    if (typeof TargetInfo.UserList[i].Number != "string") {
                        return -1;
                    }
                }
                typeG =1;
            }
        }
        if (typeG == 1 && typeU == 1) {
        /*if (typeof TargetInfo.GroupList.Number == "string" && typeof TargetInfo.UserList.Number == "string") {*/
            var msg = new Object;
            msg.Msg = "PttLoadChannelReq";
            msg.CallMode = CallMode;
            msg.GroupList = TargetInfo.GroupList;
            msg.UserList = TargetInfo.UserList;
            msg.ChannelID = ScsConstants.VoipCallID++;
            DispatchCtrlLib.WsSend(msg);
            return msg.ChannelID;
        } else {
            return -1;
        }
    }
}

//释放通道
DispatchCtrlLib.PttReleaseChannel = function(ChannelID) {
    if (typeof ChannelID == "number") {
        var msg = new Object;
        msg.Msg = "PttReleaseChannelReq";
        msg.ChannelID = ChannelID;
        DispatchCtrlLib.WsSend(msg);
        return 0;
    } else {
        return -1;
    }
}

//开始/取消监听
DispatchCtrlLib.PttMonitor = function(Type,ChannelID) {
    if (typeof ChannelID == "number" && typeof Type == "number") {
        var msg = new Object;
        msg.Msg = "PttMonitorReq";
        msg.ChannelID = ChannelID;
        msg.Type = Type;
        DispatchCtrlLib.WsSend(msg);
        return 0;
    } else {
        return -1;
    }
}

//发起/结束呼叫
DispatchCtrlLib.PttCall = function(ChannelID, Type) {
    if (typeof ChannelID == "number" && typeof Type == "number") {
        var msg = new Object;
        msg.Msg = "PttCallReq";
        msg.ChannelID = ChannelID;
        msg.Type = Type;
        DispatchCtrlLib.WsSend(msg);
        return 0;
    } else {
        return -1;
    }
}

//申请话权
DispatchCtrlLib.PttTalkRequest = function(ChannelID) {
    if (typeof ChannelID == "number") {
        var msg = new Object;
        msg.Msg = "PttTalkRequestReq";
        msg.ChannelID = ChannelID;
        DispatchCtrlLib.WsSend(msg);
        return 0;
    } else {
        return -1;
    }
}

//发起呼叫并申请话权
DispatchCtrlLib.PttCallTalkRequest = function(ChannelID) {
    if (typeof ChannelID == "number") {
        var msg = new Object;
        msg.Msg = "PttCallTalkRequestReq";
        msg.ChannelID = ChannelID;
        DispatchCtrlLib.WsSend(msg);
        return 0;
    } else {
        return -1;
    }
}

//释放话权
DispatchCtrlLib.PttTalkRelease = function(ChannelID) {
    if (typeof ChannelID == "number") {
        var msg = new Object;
        msg.Msg = "PttTalkReleaseReq";
        msg.ChannelID = ChannelID;
        DispatchCtrlLib.WsSend(msg);
        return 0;
    } else {
        return -1;
    }
}

//设置对讲音量
DispatchCtrlLib.PttSetVolume = function(ChannelID, Volume) {
    if (typeof ChannelID == "number" && typeof Volume == "number") {
        var msg = new Object;
        msg.Msg = "PttSetVolumeReq";
        msg.ChannelID = ChannelID;
        msg.Volume = Volume;
        DispatchCtrlLib.WsSend(msg);
        return 0;
    } else {
        return -1;
    }
}

//通道设置声道
DispatchCtrlLib.PttSetTrack = function(ChannelID, Track) {
    if (typeof ChannelID == "number" && typeof Track == "number") {
        var msg = new Object;
        msg.Msg = "PttSetTrackReq";
        msg.ChannelID = ChannelID;
        msg.Track = Track;
        DispatchCtrlLib.WsSend(msg);
        return 0;
    } else {
        return -1;
    }
}

/************************** 定位 ******************************/
//开始订阅位置
DispatchCtrlLib.LocStartSubscribe = function(Number, Type, Length, Interval) {
    if (typeof Number == "string" && typeof Type == "number" && typeof Length == "number" && typeof Interval ==
        "number") {
        var msg = new Object;
        msg.Msg = "LocStartSubscribeReq";
        msg.Number = Number;
        msg.Type = Type;
        msg.Length = Length;
        msg.Interval = Interval;
        DispatchCtrlLib.WsSend(msg);
        return 0;
    } else {
        return -1;
    }
}

//结束订阅位置
DispatchCtrlLib.LocStopSubscribe = function(Number) {
    if (typeof Number == "string") {
        var msg = new Object;
        msg.Msg = "LocStopSubscribeReq";
        msg.Number = Number;
        DispatchCtrlLib.WsSend(msg);
        return 0;
    } else {
        return -1;
    }
}

//终端定位参数设置
DispatchCtrlLib.LocSetParm = function(Number, ID, Type, Interval) {
    if (typeof Number == "string" && typeof ID == "number" && typeof Type == "number" && typeof Interval ==
        "number") {
        var msg = new Object;
        msg.Msg = "LocSetParmReq";
        msg.Number = Number;
        msg.ID = ID;
        msg.Type = Type;
        msg.Interval = Interval;
        DispatchCtrlLib.WsSend(msg);
        return 0;
    } else {
        return -1;
    }
}

//电子围栏告警处理
DispatchCtrlLib.FenceHandle = function(AlarmID) {
    if (typeof AlarmID == "number") {
        var msg = new Object;
        msg.Msg = "FenceHandleReq";
        msg.AlarmID = AlarmID;
        DispatchCtrlLib.WsSend(msg);
        return 0;
    } else {
        return -1;
    }
}

/************************** 状态&消息 ******************************/
//订阅终端状态
DispatchCtrlLib.StatusSubscribe = function(Type, UserList) {
    // for(var i=0;i<UserList.length;i++){
    //     if(typeof UserList[i].UserID !== "number"){
    //         return -1;
    //     }
    // }
    /*var UserList=UserList.split(",");
    for(var i=0;i<UserList.length;i++){
        UserList[i]=parseInt(UserList[i]);
    }*/
    if (typeof Type == "number" ) {
        var msg = new Object;
        msg.Msg = "StatusSubscribeReq";
        msg.Type = Type;
        msg.UserList = UserList;
        DispatchCtrlLib.WsSend(msg);
        return 0;
    } else {
        return -1;
    }
}

//发送多媒体消息
DispatchCtrlLib.MsgSend = function(TargetType, TargetInfo, MsgType, MsgInfo) {
    if (typeof TargetType == "number" && typeof MsgType == "number") {
        var msg = new Object;
        msg.Msg = "MsgSendReq";
        msg.TargetType = TargetType;
        // msg.TargetInfo = new Object;
        msg.TargetInfo = TargetInfo;
        if (TargetType == 0 || TargetType == 2) {
            if (typeof TargetInfo.UserID == "number" &&
                typeof TargetInfo.UserNumber == "string" &&
                typeof TargetInfo.UserName == "string") {
                msg.TargetInfo.UserID = TargetInfo.UserID;
                msg.TargetInfo.UserNumber = TargetInfo.UserNumber;
                msg.TargetInfo.UserName = TargetInfo.UserName;
            } else {
                return null;
            }
        }
        else if (TargetType == 1) {
            if (typeof TargetInfo.GroupID == "number" &&
                typeof TargetInfo.GroupNumber == "string" &&
                typeof TargetInfo.GroupName == "string") {
                msg.TargetInfo.GroupID = TargetInfo.GroupID;
                msg.TargetInfo.GroupNumber = TargetInfo.GroupNumber;
                msg.TargetInfo.GroupName = TargetInfo.GroupName;
                msg.TargetInfo.NickName = TargetInfo.NickName;
            } else {
                return null;
            }
        }
        else if (TargetType == 3) {
            if (typeof TargetInfo.TempGroupID == "string" &&
                typeof TargetInfo.GroupNumberList.GroupNumber == "string" &&
                typeof TargetInfo.UserNumberList.UserNumber == "string") {
                msg.TargetInfo.TempGroupID = TargetInfo.TempGroupID;
                msg.TargetInfo.GroupNumberList = TargetInfo.GroupNumberList;
                msg.TargetInfo.UserNumberList = TargetInfo.UserNumberList;
            } else {
                return null;
            }
        }
        else{
            return null;
        }

				//校验参数
        if (MsgType == 0) {
            if (typeof MsgInfo.MsgString != "string") {
                return null;
            }
        }
        else if (MsgType == 1 || MsgType == 2 || MsgType == 3 || MsgType == 5 || MsgType == 7) {
            if (typeof MsgInfo.MsgAttachAddr != "string") {
                return null;
            }
        }
        else if (MsgType == 4) {
            if (typeof MsgInfo.LatitudeType != "number" ||
                typeof MsgInfo.Latitude != "number" ||
                typeof MsgInfo.LongitudeType != "number" ||
                typeof MsgInfo.Longitude != "number" ||
                typeof MsgInfo.LocDescription != "string") {
                return null;
            }
        }
        else{
            return null;
        }

        msg.MsgType  = MsgType;
        msg.MsgInfo = MsgInfo;

        function guid() {
            function S4() {
                return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
            }
            return (S4() + S4() + S4() + S4() + S4() + S4() + S4() + S4());
        }

        msg.MsgIndex = guid();
        DispatchCtrlLib.WsSend(msg);

        return msg;
    } else {
        return null;
    }
}

//撤回多媒体消息
DispatchCtrlLib.MsgWithdraw = function(MsgIndex, TargetType, TargetInfo) {
    if (typeof MsgIndex == "string" && typeof TargetType == "number") {
        var msg = new Object;
        msg.Msg = "MsgSendReq";
        msg.MsgIndex = MsgIndex;
        msg.TargetType = TargetType;
        msg.TargetInfo = TargetInfo;
        if (TargetType == 0 || TargetType == 2) {
            if (typeof TargetInfo.UserID == "number" &&
                typeof TargetInfo.UserNumber == "string" &&
                typeof TargetInfo.UserName == "string") {
                msg.TargetInfo.UserID = TargetInfo.UserID;
                msg.TargetInfo.UserNumber = TargetInfo.UserNumber;
                msg.TargetInfo.UserName = TargetInfo.UserName;
            } else {
                return -1;
            }
        }
        else if (TargetType == 1) {
            if (typeof TargetInfo.GroupID == "number" &&
                typeof TargetInfo.GroupNumber == "string" &&
                typeof TargetInfo.GroupName == "string") {
                msg.TargetInfo.GroupID = TargetInfo.GroupID;
                msg.TargetInfo.GroupNumber = TargetInfo.GroupNumber;
                msg.TargetInfo.GroupName = TargetInfo.GroupName;
            } else {
                return -1;
            }
        }
        else if (TargetType == 3) {
            if (typeof TargetInfo.TempGroupID == "string" &&
                typeof TargetInfo.GroupNumberList.GroupNumber == "string" &&
                typeof TargetInfo.UserNumberList.UserNumber == "string") {
                msg.TargetInfo.TempGroupID = TargetInfo.TempGroupID;
                msg.TargetInfo.GroupNumberList = TargetInfo.GroupNumberList;
                msg.TargetInfo.UserNumberList = TargetInfo.UserNumberList;
            } else {
                return -1;
            }
        }
        else{
            return -1;
        }

        msg.MsgType = 99;
        msg.MsgInfo = new Object;

        DispatchCtrlLib.WsSend(msg);

        return 0;
    } else {
        return -1;
    }
}

//发送广播通知
DispatchCtrlLib.MsgBroadcast = function(NoticeID) {
    if (typeof NoticeID == "number") {
        var msg = new Object;
        msg.Msg = "MsgBroadcastReq";
        msg.NoticeID = NoticeID;
        DispatchCtrlLib.WsSend(msg);
        return 0;
    } else {
        return -1;
    }
}

/************************** 语音播报 ******************************/
//语音播报试听
DispatchCtrlLib.BroadcastTest = function(Type, Content, Voice) {
    if (typeof Type == "number" && typeof Content == "string" && typeof Voice == "number") {
        var msg = new Object;
        msg.Msg = "BroadcastTestReq";
        msg.Type = Type;
        msg.Content = Content;
        msg.Voice = Voice;
        DispatchCtrlLib.WsSend(msg);
        return 0;
    } else {
        return -1;
    }
}

//语音播报上传
DispatchCtrlLib.BroadcastUpload = function(Content, Voice) {
    if (typeof Content == "string" && typeof Voice == "number") {
        var msg = new Object;
        msg.Msg = "BroadcastUploadReq";
        msg.Content = Content;
        msg.Voice = Voice;
        DispatchCtrlLib.WsSend(msg);
        return 0;
    } else {
        return -1;
    }
}

//即时播报
DispatchCtrlLib.BroadcastInstant = function(Content, Voice, Times, ExtNumList) {
    for(var i=0;i<ExtNumList.length;i++){
        if(typeof ExtNumList[i].ExtNum !=="string"){
            return -1;
        }
    }
    if (typeof Times == "number" && typeof Content == "string" && typeof Voice == "number" ) {
        var msg = new Object;
        msg.Msg = "BroadcastInstantReq";
        msg.Times = Times;
        msg.Content = Content;
        msg.Voice = Voice;
        msg.ExtNumList = ExtNumList;
        DispatchCtrlLib.WsSend(msg);
        return 0;
    } else {
        return -1;
    }
}

//收听播报
DispatchCtrlLib.BroadcastListen = function(Type) {
    if (typeof Type == "number") {
        var msg = new Object;
        msg.Msg = "BroadcastListenReq";
        msg.Type = Type;
        DispatchCtrlLib.WsSend(msg);
        return 0;
    } else {
        return -1;
    }
}

/************************** 其他 ******************************/
//枚举本机通讯设备
DispatchCtrlLib.LocalDevShow = function(Type) {
    if (typeof DispatchDevices == "undefined")
    {
        return null;
    }
	return DispatchDevices;
}

//选择本机通讯设备
DispatchCtrlLib.LocalDevSet = function(Type, DevID) {
    if (typeof Type == "number" && typeof DevID == "string")
	{
        return DispatchDevices.setLocalDevice(Type, DevID);
    } else {
        return -1;
    }
}

//录像文件播放地址查询
DispatchCtrlLib.VideoFile = function(FileID, FileName) {
    if (typeof FileID == "string" && typeof FileName == "string") {
        var msg = new Object;
        msg.Msg = "VideoFileReq";
        msg.FileID = FileID;
        msg.FileName = FileName;
        DispatchCtrlLib.WsSend(msg);
        return 0;
    } else {
        return -1;
    }
}

//播放提示音
DispatchCtrlLib.PlaySoundReq = function(SoundID) {
    if (typeof SoundID == "number") {
        var msg = new Object;
        msg.Msg = "PlaySoundReq";
        msg.SoundID = SoundID;
        DispatchCtrlLib.WsSend(msg);
        return 0;
    } else {
        return -1;
    }
}

//本地日志写入
DispatchCtrlLib.Log4web = function(Log) {
    if (typeof Log == "string") {
        var msg = new Object;
        msg.Msg = "Log4webReq";
        msg.Log = Log;
        DispatchCtrlLib.WsSend(msg);
        return 0;
    } else {
        return -1;
    }
}

//窗口属性设置
DispatchCtrlLib.WinSetName = function(Name) {
    if (typeof Name == "string") {
        var msg = new Object;
        msg.Msg = "WinSetNameReq";
        msg.Name = Name;
        DispatchCtrlLib.WsSend(msg);
        return 0;
    } else {
        return -1;
    }
}

//窗口消息设置
DispatchCtrlLib.WinSendMsg = function(Target, Content) {
    if (typeof Target == "string") {
        var msg = new Object;
        msg.Msg = "WinSendMsgReq";
        msg.Target = Target;
        msg.Content = Content;
        DispatchCtrlLib.WsSend(msg);
        return 0;
    } else {
        return -1;
    }
}

//枚举本机通讯设备
DispatchCtrlLib.Local = function(Type) {
    if (typeof DispatchDevices == "undefined")
    {
        return null;
    }
	return DispatchDevices;
}
export { DispatchCtrlLib }
